﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace StuMana.Services
{
    public class DataviewService
    {
        private Services.ConnectionController retrieveData;
        public string[] columnName;// = new string[6];
        public string[] columnNameReal;// = new string[6];
        public int page;// = 0;
        public int maxpage;
        public int rowperpage;
        public List<string>[] list;

        public DataviewService() { 
            retrieveData = new Services.ConnectionController();
            columnName = new string[6];
            columnNameReal = new string[6];
            page = 0;
            maxpage=0;
            rowperpage=0;
        }

        // get some datas 
        public void setData(int _rowperpage, string[] _columnName, string[] _columnNameReal)
        {
            rowperpage = _rowperpage;
            columnName = _columnName;
            columnNameReal = _columnNameReal;
        }

        /*
         *Engineer: NhanHN         
         * initial show data on datagridview
         * time1: short-time for birthday
         * time2: long time for time in/out
         */
        public void DataOnDataGridViewInitial(DataGridView datagrid,string query, int time1, int time2, Label label)
        {

            try
            {
                datagrid.Rows.Clear();

                //string query = "SELECT * FROM manager where username=@username and pass=@pass";
                MySqlCommand cmd = new MySqlCommand(query, retrieveData.getMySqlConnection());

                // Name of columns
                list = retrieveData.selectStringType(cmd, columnName, time1, time2);

                maxpage = (list[0].Count%rowperpage!=0)?maxpage=list[0].Count/rowperpage:(list[0].Count/rowperpage-1);
                cmd = null;

                page = 0;

                viewdata(datagrid, label);

                if (!retrieveData.CloseConnection())
                {
                    MessageBox.Show("Connection closing failed.", "Error");
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                MessageBox.Show("Database error: \n" + ex.Message, "Error");
            }

        }

        // show data on datagridview
        public void viewdata(DataGridView datagrid, Label pages=null)
        {
            int offset = page * rowperpage;
            pages.Text = (page + 1).ToString() + "/" + (maxpage + 1).ToString() + " pages ("+list[0].Count.ToString() + " rows)";
            // row elements
            datagrid.Columns.Clear();

            for (int i = 0; i < columnName.Length; i++)
            {
                datagrid.Columns.Add(columnName[i].ToString(), columnNameReal[i].ToString());
            }

            if (list != null)
            {
                int rows = 0;
                for (int j = offset; j < offset + rowperpage; j++)
                {
                    if (rows < list[0].Count - page * rowperpage)
                    {
                        datagrid.Rows.Add();
                        datagrid.Rows[rows].HeaderCell.Value = (page * rowperpage + rows + 1).ToString();
                        for (int i = 0; i < columnName.Length; i++)
                        {
                            datagrid.Rows[rows].Cells[i].Value = list[i][j];
                        }
                        rows++;
                    }
                    else return;
                }
            }
        }
        public int GetPage()
        {
            return page;
        }
        public int GetMaxPage()
        {
            return maxpage;
        }
        public int PageIncre()
        {
            page++;
            return page;
        }
        public int PageDecre()
        {
            page--;
            return page;
        }
        public List<string>[] GetList()        {
            return list;
        }
    }
}
